// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname:"",
    transferID: "",
    transferIntegral: "",
    integral: 0,
    name: "",
    isclick:true,
    plugin_type:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({plugin_type:options.plugin_type||''});
    this.getName();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.isclick = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // id输入
  onChange(val) {
    var reg = /^[\+\-]?\d*?\.?\d*?$/;
    if (!reg.test(val.detail)) {
      wx.showToast({
        title: '只能输入数字',
        icon: 'none'
      });
      this.setData({
        recharge_id: ''
      });
    
      return;
    }
    this.setData({
      transferID: val.detail
    });
    if(this.data.timerId){clearTimeout(this.data.timerId)}
    this.data.timerId = setTimeout(() => {
      this.getNickName()
    }, 800);
  },
  getNickName(){
    if(this.data.transferID){
      let url = app.getNetAddresss("member.member.memberInfo");
      app._getNetWork({
        url,
        data:{uid:this.data.transferID},
        success: res => {
          if (res.data.result === 1) {
            this.setData({
              nickname:res.data.data.nickname
            });
          } else {
     
            this.setData({
              nickname:''
            });
          }
        }, fail: err => {
          console.log(err);
        }
      });

    }
  },
  onChange1(val) {
    var reg = /^[\+\-]?\d*?\.?\d*?$/;
    console.log(val.detail, '值');

    if (!reg.test(val.detail)) {
      wx.showToast({
        title: '只能输入数字',
        icon: 'none'
      });
      this.setData({
        transferIntegral: ''
      });
      return;
    }
    this.setData({
      transferIntegral: val.detail
    });
  },
  // 值输入的时候
  onChange2(val) {
    this.setData({
      recharge_value: val.detail
    });
    let num = this.data.recharge_value - this.data.recharge_value * (this.data.ratio / 100);
    num = num.toString();
    this.setData({
      arrival_count: num.replace(/([0-9]+.[0-9]{2})[0-9]*/, "$1")
    });
  },
  getName() {
    let _url = "plugin.integral.Frontend.Controllers.page.get-name";
    if(this.data.plugin_type == 'new_integral'){
      _url = "plugin.new-integral.Frontend.Controllers.page.get-name";
    }
    let urlStr = app.getNetAddresss(_url);
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            name: res.data.data
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let _url = "plugin.integral.Frontend.Controllers.Page.index";
    if(this.data.plugin_type == 'new_integral'){
      _url = "plugin.new-integral.Frontend.Controllers.Page.index";
    }
    let urlStr = app.getNetAddresss(_url);
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          console.log(res.data.data.member_integral,'数据的强');
          
          this.setData({
            transferID: '',
            transferIntegral: '',
            integral: res.data.data.member_integral
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  sureTransfer() {
    if(!this.data.isclick){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      });
      return;			
    }
    this.data.isclick = false;
    if (!this.data.transferIntegral) {
      wx.showToast({
        title: "请输入转让数额",
        icon: 'none'
      });
      this.data.isclick = true;
      return;
    }
    if (Number(this.data.transferIntegral) > Number(this.data.integral)) {
      wx.showToast({
        title: '输入的提现数额超过已有的' + this.data.name,
        icon: 'none'
      });
      this.data.isclick = true;
      return;
    }
    let _url = "plugin.integral.Frontend.Modules.Integral.Controllers.Transfer";
    if(this.data.plugin_type == 'new_integral'){
      _url = "plugin.new-integral.Frontend.Modules.Integral.Controllers.Transfer";
    }

    let urlStr = app.getNetAddresss(_url);
    urlStr += "&change_value=" + this.data.transferIntegral;
    urlStr += "&recipient_id=" + this.data.transferID;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.getData();
          wx.showToast({
            title: '转让成功',
            icon: 'none'
          });
          let path = "/packageB/member/Integral_love_detail/Integral_love_detail";
          if(this.data.plugin_type == 'new_integral'){
            path = "/packageB/member/Integral_love_detail/Integral_love_detail?plugin_type=new_integral"
          }
          setTimeout(() => {
            wx.navigateTo({
              url:path,
            });
          }, 1000);
        } else {
          this.data.isclick = true;
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});